package com.wxq.yaoxia;

import java.util.ArrayList;
import java.util.List;

import android.app.SearchManager;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.wxq.yaoxia.mainborad.YXMainboradActivity;
import com.wxq.yaoxia.util.YXConstants;
import com.wxq.yaoxia.util.YXDBHelper;

public abstract class YXBaseSearchResultActivity extends YXBaseActivity {

	protected ListView resultLV;
	protected YXDBHelper dbHelper;
	protected Cursor cursor;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		dbHelper = new YXDBHelper(this);
		
		Intent oldIntent = getIntent();
		checkIntent(oldIntent);
		setContentViewWithTopbar(R.layout.yx_search_result, R.layout.yx_topbar);
		resultLV = (ListView) findViewById(R.id.relativeLV);
		setResultListView();
		resultLV.setOnItemClickListener(
				new OnItemClickListener()
				{
					@Override
					public void onItemClick(AdapterView<?> parent, View view,
							int position, long id) {
						setLVItemClickAction(position);
					}				
				});
	}
	
	public abstract void setLVItemClickAction(int position);

	@Override
	public void setTopBarButtonListener() {
		// TODO Auto-generated method stub
		Button leftBtn = (Button) findViewById(R.id.btnLeft);
    	Button rightBtnButton = (Button) findViewById(R.id.btnRight);
    	TextView centerTV = (TextView) findViewById(R.id.centerTV);
    	
    	leftBtn.setVisibility(View.VISIBLE);
    	leftBtn.setText("返回");
    	leftBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
    	rightBtnButton.setVisibility(View.VISIBLE);
//    	rightBtnButton.setText("药");
    	rightBtnButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setClass(YXBaseSearchResultActivity.this, YXMainboradActivity.class);
				startActivity(intent);
			}
		});
    	centerTV.setVisibility(View.VISIBLE);
    	centerTV.setText("搜索结果");
	}

	public abstract Cursor getDoSearchCursor(String queryString);
	
	public void doSearchQuery(Intent queryIntent) {
		
		final String queryString = queryIntent.getStringExtra(SearchManager.QUERY);
		Log.d("wxq", "Search 获得 query:"+queryString);
		
		cursor = getDoSearchCursor(queryString);
		startManagingCursor(cursor);
		SimpleCursorAdapter adapter = new SimpleCursorAdapter(
				this, 
				R.layout.shool_item, 
				cursor, new String [] {YXConstants.COLUMN_Name}, new int [] {android.R.id.text1});
		resultLV.setAdapter(adapter);
		
	}
	
	public void doView(Intent queryIntent) {
		
		String queryString = queryIntent.getDataString();
		Log.d("wxq", "View 获得 query:"+queryString);
		List<String> list = new ArrayList<String>();
		
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.shool_item, list);
		resultLV.setAdapter(adapter);
	}
	
	public abstract Cursor getDoDefaultCursor(String queryString);
	
	public void doDefault(Intent queryIntent) {
		Log.d("wxq", "进入doDefault");
		final String queryString = queryIntent.getStringExtra(YXConstants.COLUMN_QueryId);
		cursor = getDoDefaultCursor(queryString);
		startManagingCursor(cursor);
		SimpleCursorAdapter adapter = new SimpleCursorAdapter(
				this, 
				R.layout.shool_item, 
				cursor, new String [] {YXConstants.COLUMN_Name}, new int [] {android.R.id.text1});
		resultLV.setAdapter(adapter);
	}
	
	public void setResultListView() {
		final Intent queryIntent = getIntent();
		final String queryAction = queryIntent.getAction();
		if(Intent.ACTION_SEARCH.equals(queryAction))
		{
			//点击搜索按钮进入
			doSearchQuery(queryIntent);
			return ;
		}
		else if (Intent.ACTION_VIEW.equals(queryAction)) {
			//点击建议程序列表进入
			doView(queryIntent);
			return ;
		}
		else {
			//点击药品名搜索相似进入
			doDefault(queryIntent);
			return ;
		}
	}

	@Override
	protected void onNewIntent(Intent intent) {
		// TODO Auto-generated method stub
		super.onNewIntent(intent);
		checkIntent(intent);
		setResultListView();
	}
	
	public abstract Cursor getCheckData(String queryId);
	
	public void checkIntent(Intent intent) {
		final String queryAction = intent.getAction();
		if(Intent.ACTION_VIEW.equals(queryAction))
		{
			//点击建议项进入
			String queryId = intent.getDataString();
			cursor = getCheckData(queryId);
			startManagingCursor(cursor);
			if(cursor.moveToFirst())
			{
				setLVItemClickAction(0);
			}		
			finish();
		}
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		if(cursor != null && !cursor.isClosed())
		{
			cursor.close();
		}
		if(dbHelper != null)
		{
			dbHelper.close();
		}
		super.onDestroy();
	}
}
